package com.example.asus.homework11.fragments;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.activity.UserCartActivity;
import com.example.asus.homework11.activity.UserCommentsActivity;
import com.example.asus.homework11.activity.UserDetailActivity;
import com.example.asus.homework11.activity.UserOrderActivity;
import com.example.asus.homework11.bean.UserBean;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment implements View.OnClickListener {
    private TextView topbar_title,tv_username_user,tv_changeUser_user,tv_order_user,tv_shoppingCart_user,tv_comment_user;
    private SharedPreferences sharedPreferences;
    private int userid;

    public UserFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData(view);
        initEvent(view);
    }

    private void initView(View v){
        topbar_title=v.findViewById(R.id.topbar_title);
        tv_username_user=v.findViewById(R.id.tv_username_user);
        tv_changeUser_user=v.findViewById(R.id.tv_changeUser_user);
        tv_order_user=v.findViewById(R.id.tv_order_user);
        tv_shoppingCart_user=v.findViewById(R.id.tv_shoppingCart_user);
        tv_comment_user=v.findViewById(R.id.tv_comment_user);
    }

    private void initData(View v){
        sharedPreferences = getActivity().getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        BaseModel<UserBean> model=new BaseModel<>();
        model.callEnqueue(model.service.getUser(userid),listner);
    }

    private void initEvent(View v){
        tv_changeUser_user.setOnClickListener(this);
        tv_order_user.setOnClickListener(this);
        tv_shoppingCart_user.setOnClickListener(this);
        tv_comment_user.setOnClickListener(this);
    }

    BaseListner<UserBean> listner=new BaseListner<UserBean>() {
        @Override
        public void onResponse(UserBean userBean) {
            if(null!=userBean){
                topbar_title.setText(userBean.getUsername());
                tv_username_user.setText(userBean.getUsername());
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_changeUser_user:
                startActivity(new Intent(getContext(),UserDetailActivity.class));
                break;
            case R.id.tv_order_user:
                startActivity(new Intent(getContext(),UserOrderActivity.class));
                break;
            case R.id.tv_shoppingCart_user:
                startActivity(new Intent(getContext(),UserCartActivity.class));
                break;
            case R.id.tv_comment_user:
                startActivity(new Intent(getContext(),UserCommentsActivity.class));
                break;
        }


    }
}
